package enterprise.manage.web.controller;

import enterprise.manage.entity.User;
import enterprise.manage.security.utils.IpUtil;
import enterprise.manage.security.utils.PasswordUtils;
import enterprise.manage.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: mojiajing
 * Date: 2011-7-1
 * Time: 16:29:35
 * Desc:
 */
public class UserRegisterController extends SimpleFormController {
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRegisterController.class);

    @Autowired
    private UserService userService;

    private static final String ERROR_VIEW = "user/error";

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        UserRegisterCommand userRegisterCommand = (UserRegisterCommand) command;
        String userName = userRegisterCommand.getUserName();
        String realName = userRegisterCommand.getRealName();
        String userPassword = userRegisterCommand.getUserPassword();
        String phone = userRegisterCommand.getPhone();
        String email = userRegisterCommand.getEmail();

        if (StringUtils.isBlank(userName)) {
            request.setAttribute("error", "用户名不能为空");
            request.setAttribute("userRegisterCommand", userRegisterCommand);
            return new ModelAndView(this.getFormView());
        }
        if (StringUtils.isBlank(userPassword)) {
            request.setAttribute("error", "密码不能为空");
            request.setAttribute("userRegisterCommand", userRegisterCommand);
            return new ModelAndView(this.getFormView());
        }
        if (StringUtils.isBlank(realName)) {
            request.setAttribute("error", "真实姓名不能为空");
            request.setAttribute("userRegisterCommand", userRegisterCommand);
            return new ModelAndView(this.getFormView());
        }
        if (StringUtils.isBlank(email)) {
            request.setAttribute("error", "电子邮箱不能为空");
            request.setAttribute("userRegisterCommand", userRegisterCommand);
            return new ModelAndView(this.getFormView());
        }

        String regIp = IpUtil.getCookieIp(request);
        userPassword = PasswordUtils.passwordEncrypt(userName, userPassword);
        User user = new User();
        user.setUserName(userName.trim());
        user.setRealName(realName.trim());
        user.setPsw(userPassword);
        user.setMobile(phone);
        user.setEmail(email);
        Date date = new Date();
        user.setCreateTime(date);
        user.setModifyTime(date);
        user.setDelFlag(0);
        user.setEnable(0);
        user.setRegIp(regIp);
        boolean flag = userService.checkUserName(user.getUserName());
        if (flag) {
            request.setAttribute("error", "该用户名不可用");
            return new ModelAndView(this.getFormView());
        } else {
            userService.registerUser(user);
            return new ModelAndView(this.getSuccessView());
        }
    }

    @Override
    protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("error", "请勿重复提交");
        return new ModelAndView(this.getFormView());
    }


}
